<?php

include("connect.php");
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
function charityExists($charityName, $dblink) {


	$result = mysql_query(sprintf("SELECT `ID` FROM Charity WHERE Name='%s'", 	 mysql_real_escape_string($charityName)));
	if (!$result) {
	    die('Could not query:' . mysql_error());
	}
	if(mysql_num_rows($result)!=0) {
		$value = mysql_result($result, 0); // outputs third employee's name
	
		if($value != "" && $value != NULL) {
			return $value;
		} else {
			return insertCharity($charityName, $dblink);
		}

	} else {
		return insertCharity($charityName, $dblink);
	}
}

function insertCharity($charityName, $dblink) {


	$query = sprintf("INSERT INTO Charity(`Name`) VALUES ('%s')",  mysql_real_escape_string($charityName));

	$result = mysql_query($query, $dblink);  

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

return  mysql_insert_id();

}


function getPublicationID($pdfname, $dblink) {


$query = sprintf("SELECT `ID` FROM Publication WHERE `Name` = '%s'", mysql_real_escape_string($pdfname));

	 $result = mysql_query($query, $dblink);



	

	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	    	$message .= 'Whole query: ' . $query;
    		die($message);
	}
$value = "";
if(mysql_num_rows($result)!=0) {
		$value = mysql_result($result, 0); // outputs third employee's name

}
	return $value;
	


}


function insertNewPublication($country, $datepublished, $pdfname, $noticetype, $dblink) {
	


	$blah = getPublicationID($pdfname, $dblink);

	if($blah != "" && $blah != null) {
		return $blah;
	} else {

	$query = sprintf("INSERT INTO Publication (`Name`, `NotificationID`, `DatePublished`, `CountryID`) VALUES ('%s', %s, '%s', %s)", 
				mysql_real_escape_string($pdfname),
				mysql_real_escape_string($noticetype), 
				mysql_real_escape_string($datepublished), 
				mysql_real_escape_string($country));

	 $result = mysql_query($query, $dblink);

	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	    	$message .= 'Whole query: ' . $query;
    		die($message);
	}

	return  mysql_insert_id();
}
}


function getDateOfRecord($value) {


$blah = substr($value, 4, 4);     // bcdef
$blah =  $blah."-".substr($value, 2, 2);
$blah = $blah."-".substr($value, 0, 2);
  
return $blah;


}



function insertNewPerson(
	$surname, 
	$firstnames, 
	$formername, 
	$neename, 
	$alsoknownas, 
	$deathdate, 
	$birthdate, 
	$age, 
	$agemeasure, 
	$cityid, 
	$formercityid, 
	$regionid, 
	$countryid, 
	$charityid, 
	$funeralhomeid, 
	$otherinfo, $dblink) {

	$query = sprintf("INSERT INTO Person(
						`Surname`, 
						`FirstNames`, 
						`FormerName`, 
						`NeeName`, 
						`AlsoKnownAs`, 
						`DeathDate`, 
						`BirthDate`, 
						`Age`, 
						`AgeMeasure`, 
						`CityTownID`, 
						`FormerCityTownID`, 
						`RegionID`, 
						`CountryID`, 
						`CharityID`, 
						`FuneralHomeID`, 
						`OtherInfo`) 
					VALUES (
						'%s',
						'%s',
						'%s', 
						'%s', 
						'%s', 
						'%s', 
						'%s', 
						'%s', 
						'%s', 
						%s, 
						%s, 
						%s, 
						%s, 
						%s, 
						%s, 
						'%s')", 
					mysql_real_escape_string($surname),
					mysql_real_escape_string($firstnames), 
					mysql_real_escape_string($formername), 
					mysql_real_escape_string($neename), 
					mysql_real_escape_string($alsoknownas), 
					mysql_real_escape_string($deathdate), 
					mysql_real_escape_string($birthdate), 
					mysql_real_escape_string($age), 
					mysql_real_escape_string($agemeasure), 
					mysql_real_escape_string($cityid), 
					mysql_real_escape_string($formercityid),
					mysql_real_escape_string($regionid),
					mysql_real_escape_string($countryid),
					mysql_real_escape_string($charityid),
					mysql_real_escape_string($funeralhomeid),
					mysql_real_escape_string($otherinfo));

	
$result = mysql_query($query, $dblink);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

return  mysql_insert_id();

}


function insertNewPublicationPerson($personid, $publicationid, $dblink) {

	$query = sprintf("INSERT INTO PublicationPerson (`PersonID`, `PublicationID`) VALUES (%s, %s)", mysql_real_escape_string($personid),
					mysql_real_escape_string($publicationid));

 

$result = mysql_query($query, $dblink);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    echo $message;
}

return  mysql_insert_id();



}







if($_POST["surname"] != "" && $_POST["surname"] != NULL) {
$charityid = charityExists($_POST["charity"], $link);

$personid = insertNewPerson($_POST["surname"], $_POST["firstname"], $_POST["formername"], $_POST["neename"], $_POST["alsoknownas"], $_POST["deathdate"], $_POST["birthdate"], $_POST["age"], $_POST["agemeasure"], $_POST["citytown"], $_POST["formercitytown"], $_POST["region"], $_POST["country"], $charityid, $_POST["funeralhome"], $_POST["othernotes"], $link);


if($_POST["pdf1"] != "" && $_POST["pdf1"] != NULL) {
$data = $_POST["pdf1"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}
if($_POST["pdf2"] != "" && $_POST["pdf2"] != NULL) {
$data = $_POST["pdf2"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}
if($_POST["pdf3"] != "" && $_POST["pdf3"] != NULL) {
$data = $_POST["pdf3"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}
if($_POST["pdf4"] != "" && $_POST["pdf4"] != NULL) {
$data = $_POST["pdf4"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}
if($_POST["pdf5"] != "" && $_POST["pdf5"] != NULL) {
$data = $_POST["pdf5"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}
if($_POST["pdf6"] != "" && $_POST["pdf6"] != NULL) {
$data = $_POST["pdf6"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}
if($_POST["pdf7"] != "" && $_POST["pdf7"] != NULL) {
$data = $_POST["pdf7"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}
if($_POST["pdf8"] != "" && $_POST["pdf8"] != NULL) {
$data = $_POST["pdf8"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}
if($_POST["pdf9"] != "" && $_POST["pdf9"] != NULL) {
$data = $_POST["pdf9"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}
if($_POST["pdf10"] != "" && $_POST["pdf10"] != NULL) {
$data = $_POST["pdf10"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}

if($_POST["pdf11"] != "" && $_POST["pdf11"] != NULL) {

$data = $_POST["pdf11"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}


if($_POST["pdf12"] != "" && $_POST["pdf12"] != NULL) {

$data = $_POST["pdf12"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}

if($_POST["pdf13"] != "" && $_POST["pdf13"] != NULL) {

$data = $_POST["pdf13"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}

if($_POST["pdf14"] != "" && $_POST["pdf14"] != NULL) {

$data = $_POST["pdf14"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}

if($_POST["pdf15"] != "" && $_POST["pdf15"] != NULL) {

$data = $_POST["pdf15"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}

if($_POST["pdf16"] != "" && $_POST["pdf16"] != NULL) {

$data = $_POST["pdf16"];
list($death, $notice, $shortcode, $date) = explode("-", $data);

$publicationid = insertNewPublication(412, getDateOfRecord($date), $data, 1, $link);

insertNewPublicationPerson($personid, $publicationid, $link);
}



echo $personid;

}





// Free the resources associated with the result set
// This is done automatically at the end of the script
//mysql_free_result($result);
mysql_close($link);
?>
